/*
 * @Descripttion: 
 * @version: V1.0.0
 * @Author: LeonHe
 * @Date: 2021-10-09 00:01:44
 * @LastEditTime: 2021-10-10 19:58:33
 */

#include "time.h"

uint32_t gTimCountMs = 0;

void timeCoutStart(stTimeObjDef* pObj, uint32_t timeOut)
{
    pObj->lastTime = gTimCountMs;
    pObj->timeOutNum = timeOut;
    pObj->isTimeOut = 0;
}

uint8_t timeIsTimeOut(stTimeObjDef* pObj)
{
    uint8_t  isTimeOut = 0;
    uint32_t timeDiff = 0;
    uint32_t timeCurr = gTimCountMs;
    if(timeCurr < pObj->lastTime)
    {
        timeDiff = 0xFFFFFFFF - pObj->lastTime + timeCurr;
    }
    else
    {
        timeDiff = timeCurr - pObj->lastTime;
    }

    if(timeDiff >= pObj->timeOutNum)
    {
        pObj->isTimeOut = 1;
        isTimeOut = 1;
    }

    return isTimeOut;
}

void timeDelayMs(uint32_t ms)
{
    uint32_t lastTime = gTimCountMs;
    uint32_t timeDiff = 0;
    uint32_t timeCurr = gTimCountMs;

    while(1)
    {
        timeCurr = gTimCountMs;
        if(timeCurr < lastTime)
        {
            timeDiff = 0xFFFFFFFF - lastTime + timeCurr;
        }
        else
        {
            timeDiff = timeCurr - lastTime;
        }

        if(timeDiff >= ms)
        {
            break;
        }
    }

    return;
}


void delayUs(uint16_t us)
{
    for(;us > 0; us--)
    {
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
        _asm("nop");
    }
}

void delayMs(uint16_t ms)
{
    for(;ms > 0; ms--)
    {
        delayUs(880);
    }
}

